{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9e456a59-8edb-4855-9b70-19046994f991",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-22T07:17:07.587513Z",
     "iopub.status.busy": "2025-11-22T07:17:07.587293Z",
     "iopub.status.idle": "2025-11-22T07:17:07.592969Z",
     "shell.execute_reply": "2025-11-22T07:17:07.592563Z",
     "shell.execute_reply.started": "2025-11-22T07:17:07.587492Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "llm 调用 tools,让大模型和外界交流\n"
     ]
    }
   ],
   "source": [
    "print(\"llm 调用 tools,让大模型和外界交流\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0a73782f-93dc-4534-bec5-23eaff8bb3b6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-22T07:17:08.781192Z",
     "iopub.status.busy": "2025-11-22T07:17:08.780907Z",
     "iopub.status.idle": "2025-11-22T07:17:17.530090Z",
     "shell.execute_reply": "2025-11-22T07:17:17.529583Z",
     "shell.execute_reply.started": "2025-11-22T07:17:08.781168Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: requests in d:\\python\\lib\\site-packages (2.32.4)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in d:\\python\\lib\\site-packages (from requests) (3.4.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in d:\\python\\lib\\site-packages (from requests) (3.10)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\python\\lib\\site-packages (from requests) (2.5.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\python\\lib\\site-packages (from requests) (2025.6.15)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 25.0.1 -> 25.3\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting openai\n",
      "  Downloading openai-2.8.1-py3-none-any.whl.metadata (29 kB)\n",
      "Collecting anyio<5,>=3.5.0 (from openai)\n",
      "  Downloading anyio-4.11.0-py3-none-any.whl.metadata (4.1 kB)\n",
      "Collecting distro<2,>=1.7.0 (from openai)\n",
      "  Downloading distro-1.9.0-py3-none-any.whl.metadata (6.8 kB)\n",
      "Collecting httpx<1,>=0.23.0 (from openai)\n",
      "  Downloading httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)\n",
      "Collecting jiter<1,>=0.10.0 (from openai)\n",
      "  Downloading jiter-0.12.0-cp313-cp313-win_amd64.whl.metadata (5.3 kB)\n",
      "Collecting pydantic<3,>=1.9.0 (from openai)\n",
      "  Downloading pydantic-2.12.4-py3-none-any.whl.metadata (89 kB)\n",
      "Collecting sniffio (from openai)\n",
      "  Downloading sniffio-1.3.1-py3-none-any.whl.metadata (3.9 kB)\n",
      "Collecting tqdm>4 (from openai)\n",
      "  Downloading tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)\n",
      "Collecting typing-extensions<5,>=4.11 (from openai)\n",
      "  Downloading typing_extensions-4.15.0-py3-none-any.whl.metadata (3.3 kB)\n",
      "Requirement already satisfied: idna>=2.8 in d:\\python\\lib\\site-packages (from anyio<5,>=3.5.0->openai) (3.10)\n",
      "Requirement already satisfied: certifi in d:\\python\\lib\\site-packages (from httpx<1,>=0.23.0->openai) (2025.6.15)\n",
      "Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai)\n",
      "  Downloading httpcore-1.0.9-py3-none-any.whl.metadata (21 kB)\n",
      "Collecting h11>=0.16 (from httpcore==1.*->httpx<1,>=0.23.0->openai)\n",
      "  Downloading h11-0.16.0-py3-none-any.whl.metadata (8.3 kB)\n",
      "Collecting annotated-types>=0.6.0 (from pydantic<3,>=1.9.0->openai)\n",
      "  Downloading annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n",
      "Collecting pydantic-core==2.41.5 (from pydantic<3,>=1.9.0->openai)\n",
      "  Downloading pydantic_core-2.41.5-cp313-cp313-win_amd64.whl.metadata (7.4 kB)\n",
      "Collecting typing-inspection>=0.4.2 (from pydantic<3,>=1.9.0->openai)\n",
      "  Downloading typing_inspection-0.4.2-py3-none-any.whl.metadata (2.6 kB)\n",
      "Requirement already satisfied: colorama in d:\\python\\lib\\site-packages (from tqdm>4->openai) (0.4.6)\n",
      "Downloading openai-2.8.1-py3-none-any.whl (1.0 MB)\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.3/1.0 MB ? eta -:--:--\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   -------------------- ------------------- 0.5/1.0 MB 53.1 kB/s eta 0:00:10\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ------------------------------ --------- 0.8/1.0 MB 45.9 kB/s eta 0:00:06\n",
      "   ---------------------------------------- 1.0/1.0 MB 49.1 kB/s eta 0:00:00\n",
      "Downloading anyio-4.11.0-py3-none-any.whl (109 kB)\n",
      "Downloading distro-1.9.0-py3-none-any.whl (20 kB)\n",
      "Downloading httpx-0.28.1-py3-none-any.whl (73 kB)\n",
      "Downloading httpcore-1.0.9-py3-none-any.whl (78 kB)\n",
      "Downloading jiter-0.12.0-cp313-cp313-win_amd64.whl (204 kB)\n",
      "Downloading pydantic-2.12.4-py3-none-any.whl (463 kB)\n",
      "Downloading pydantic_core-2.41.5-cp313-cp313-win_amd64.whl (2.0 MB)\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   ---------- ----------------------------- 0.5/2.0 MB 45.8 kB/s eta 0:00:33\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   --------------- ------------------------ 0.8/2.0 MB 44.3 kB/s eta 0:00:28\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   -------------------- ------------------- 1.0/2.0 MB 45.2 kB/s eta 0:00:22\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------- -------------- 1.3/2.0 MB 42.1 kB/s eta 0:00:17\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------- -------- 1.6/2.0 MB 44.9 kB/s eta 0:00:10\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ------------------------------------ --- 1.8/2.0 MB 42.1 kB/s eta 0:00:05\n",
      "   ---------------------------------------- 2.0/2.0 MB 46.2 kB/s eta 0:00:00\n",
      "Downloading sniffio-1.3.1-py3-none-any.whl (10 kB)\n",
      "Downloading tqdm-4.67.1-py3-none-any.whl (78 kB)\n",
      "Downloading typing_extensions-4.15.0-py3-none-any.whl (44 kB)\n",
      "Downloading annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",
      "Downloading typing_inspection-0.4.2-py3-none-any.whl (14 kB)\n",
      "Downloading h11-0.16.0-py3-none-any.whl (37 kB)\n",
      "Installing collected packages: typing-extensions, tqdm, sniffio, jiter, h11, distro, annotated-types, typing-inspection, pydantic-core, httpcore, anyio, pydantic, httpx, openai\n",
      "Successfully installed annotated-types-0.7.0 anyio-4.11.0 distro-1.9.0 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 jiter-0.12.0 openai-2.8.1 pydantic-2.12.4 pydantic-core-2.41.5 sniffio-1.3.1 tqdm-4.67.1 typing-extensions-4.15.0 typing-inspection-0.4.2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 25.0.1 -> 25.3\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "# npm install\n",
    "# 发送请求\n",
    "!pip  install requests\n",
    "!pip install openai\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5721c77b-3bf4-4691-9f4d-cbd125231ca2",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-11-22T07:29:42.880231Z",
     "iopub.status.busy": "2025-11-22T07:29:42.879794Z",
     "iopub.status.idle": "2025-11-22T07:29:45.195758Z",
     "shell.execute_reply": "2025-11-22T07:29:45.195346Z",
     "shell.execute_reply.started": "2025-11-22T07:29:42.880206Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'results': [{'location': {'id': 'WSFXR95RZD21', 'name': '抚州', 'country': 'CN', 'path': '抚州,抚州,江西,中国', 'timezone': 'Asia/Shanghai', 'timezone_offset': '+08:00'}, 'now': {'text': '晴', 'code': '0', 'temperature': '19'}, 'last_update': '2025-11-22T15:57:17+08:00'}]}\n",
      "抚州当前天气：晴, 气温 19度\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "# python 类型约定，\n",
    "# js 弱类型语言 \n",
    "# -> str 返回值的类型\n",
    "def get_weather(location: str) -> str:\n",
    "    url = \"https://api.seniverse.com/v3/weather/now.json\"\n",
    "    params = {\n",
    "        \"key\": \"SaVSOt7sYbwpka9iv\",\n",
    "        \"location\": location,\n",
    "        \"language\": \"zh-Hans\"\n",
    "    }\n",
    "    try:\n",
    "        resp = requests.get(url, params=params, timeout=10)\n",
    "        data = resp.json()\n",
    "        print(data)\n",
    "        if \"results\" in data:\n",
    "            r = data[\"results\"][0]\n",
    "            city = r[\"location\"][\"name\"]\n",
    "            # 当前天气对象\n",
    "            now = r[\"now\"]\n",
    "            text = now[\"text\"]\n",
    "            temp = now[\"temperature\"]\n",
    "            #python 擅长机器学习 和字符处理\n",
    "            return f\"{city}当前天气：{text}, 气温 {temp}度\"\n",
    "        else:\n",
    "            return \"查询失败\"\n",
    "    except Exception as e:\n",
    "        return  f\"异常：{e}\"\n",
    "    \n",
    "print(get_weather(\"抚州\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "682aa3cb-1623-4201-918c-a62db6512396",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-22T07:35:56.107587Z",
     "iopub.status.busy": "2025-11-22T07:35:56.107374Z",
     "iopub.status.idle": "2025-11-22T07:35:56.532697Z",
     "shell.execute_reply": "2025-11-22T07:35:56.532258Z",
     "shell.execute_reply.started": "2025-11-22T07:35:56.107572Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 用户user向llm 提问 自然语言\n",
    "# 实时性的， 工具类的， llm 无法回答\n",
    "# llm和原有的互联网文明桥接起来？ 智能互联网来了\n",
    "# tools 来自openai 接口定义，\n",
    "from openai import OpenAI\n",
    "\n",
    "client = OpenAI(\n",
    "    api_key='sk-11df48a9e17e4da18a24d1a51f32ae35',\n",
    "    base_url='https://api.deepseek.com/v1'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7eae9531-7c98-42ed-86ac-1777bf779a96",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-11-22T07:42:26.383878Z",
     "iopub.status.busy": "2025-11-22T07:42:26.383653Z",
     "iopub.status.idle": "2025-11-22T07:42:26.386906Z",
     "shell.execute_reply": "2025-11-22T07:42:26.386500Z",
     "shell.execute_reply.started": "2025-11-22T07:42:26.383864Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# openai 定义接口\n",
    "tools = [\n",
    "    # JSON 定义\n",
    "    {\n",
    "        # 工具就是一个函数\n",
    "        \"type\":\"function\",\n",
    "        \"function\":{\n",
    "            \"name\": \"get_weather\",\n",
    "            # 自然语言处理\n",
    "            \"description\": \"获取指定城市的当前天气\",\n",
    "            \"parameters\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"location\": {\n",
    "                        \"type\": \"string\",\n",
    "                        # 北京天气怎么样？提取出来北京\n",
    "                        \"description\": \"城市名称，如'北京'\"\n",
    "                    }\n",
    "                },\n",
    "                \"required\": [\"name\"]\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6fa1b6b3-b295-4473-b9fc-d19965cbb349",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChatCompletionMessage(content='我来帮您查询北京的天气情况。', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=[ChatCompletionMessageFunctionToolCall(id='call_00_oz7dyjDGHorDxJ4Sk6h8gufL', function=Function(arguments='{\"location\": \"北京\"}', name='get_weather'), type='function', index=0)])\n",
      "{'results': [{'location': {'id': 'WX4FBXXFKE4F', 'name': '北京', 'country': 'CN', 'path': '北京,北京,中国', 'timezone': 'Asia/Shanghai', 'timezone_offset': '+08:00'}, 'now': {'text': '晴', 'code': '0', 'temperature': '14'}, 'last_update': '2025-11-22T15:57:49+08:00'}]}\n",
      "根据查询结果，北京当前天气情况如下：\n",
      "\n",
      "**北京现在天气**：\n",
      "- **天气状况**：晴朗 ☀️\n",
      "- **当前温度**：14°C\n",
      "\n",
      "今天天气不错，是个晴朗的日子，温度也比较舒适。如果您需要了解未来几天的天气预报，或者想了解其他地区的天气情况，随时告诉我！\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "messages = [{\"role\": \"user\", \"content\": \"北京天气怎么样\"}]\n",
    "response = client.chat.completions.create(\n",
    "    model = 'deepseek-reasoner',\n",
    "    messages = messages,\n",
    "    tools = tools,\n",
    "    tool_choice = \"auto\",\n",
    "    # 生成的随机度\n",
    "    temperature = 0.3\n",
    ")\n",
    "response_message = response.choices[0].message\n",
    "# 多轮会话\n",
    "print(response_message)\n",
    "# tool_calls ChatCompletionMessageFunctionToolCall\n",
    "messages.append(response_message)\n",
    "if response_message.tool_calls:\n",
    "    for tool_call in response_message.tool_calls:\n",
    "        function_name = tool_call.function.name\n",
    "        # json 字符串变成json 对象\n",
    "        function_args = json.loads(tool_call.function.arguments)\n",
    "        if function_name == \"get_weather\":\n",
    "            function_response = get_weather(function_args[\"location\"])\n",
    "        else:\n",
    "            function_response = \"未知工具\"\n",
    "        messages.append({\n",
    "            \"tool_call_id\": tool_call.id,\n",
    "            \"role\": \"tool\",\n",
    "            \"name\": function_name,\n",
    "            \"content\": function_response\n",
    "        })\n",
    "else:\n",
    "    print(response_message.content)\n",
    "\n",
    "final_response = client.chat.completions.create(\n",
    "    model=\"deepseek-reasoner\",\n",
    "    messages=messages,\n",
    "    temperature=0.3\n",
    ")\n",
    "print(final_response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e74028a3-de98-4d82-a9c7-7096fcb7499b",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a4645202-8323-4993-8af1-a6012946f3d5",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
